package com.company.project.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.company.project.common.utils.RestUtil;
import com.company.project.pojo.Rest;
import com.company.project.pojo.ServiceCode;
import com.company.project.pojo.permission.Permission;
import com.company.project.pojo.rolePermission.RolePermission;
import com.company.project.service.IPermissionService;
import com.company.project.service.IRolePermissionService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

/**
 * <p>
 * 权限 前端控制器
 * </p>
 *
 * @author 任建波
 * @since 2019-08-10
 */
@Api(value = "权限",tags = "权限")
@Controller
@RequestMapping("permission")
public class PermissionController {

    @Autowired
    HttpServletRequest request;
    @Autowired
    private IPermissionService iPermissionService;
    @Autowired
    private IRolePermissionService iRolePermissionService;

    @ApiOperation(value = "获取权限列表")
    @RequestMapping(value = "getPermissionList", method = RequestMethod.GET)
    @ResponseBody
    public Rest getPermissionList(){
        QueryWrapper<Permission> queryWrapper = new QueryWrapper<>();

        List<Permission> permissionList = iPermissionService.list(queryWrapper);

        return RestUtil.rest(ServiceCode.CODE_200, "成功！！！" , permissionList);
    }

    @ApiOperation(value = "根据角色id获取权限列表")
    @RequestMapping(value = "getPermissionListByRoleId", method = RequestMethod.GET)
    @ResponseBody
    public Rest getPermissionListByRoleId(int roleId){
        QueryWrapper<RolePermission> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("role_id", roleId);

        List<RolePermission> rolePermissionList = iRolePermissionService.list(queryWrapper);

        List<Integer> permissionIds = new ArrayList();
        for (RolePermission rolePermission: rolePermissionList){
            permissionIds.add(rolePermission.getPermissionId());
        }

        Collection<Permission> permissionList = new ArrayList<>();
        if(permissionIds.size() != 0){
            permissionList = iPermissionService.listByIds(permissionIds);
        }

        return RestUtil.rest(ServiceCode.CODE_200, "成功！！！" , permissionList);
    }

}

